/*
	Written By Pradipna Nepal
	www.pradsprojects.com

	Copyright (C) 2011 Pradipna Nepal
	Please read COPYING.txt included along with this source code for more detail.
	If not included, see http://www.gnu.org/licenses/
*/

#include "libFrameTimer.h"

FrameTimer::FrameTimer() {
	TIMECAPS timeCaps;
	unsigned int maxResolution;
	
	//Find out the maximum resolution possible in the hardware
	if (timeGetDevCaps(&timeCaps, sizeof(timeCaps)) != MMSYSERR_NOERROR)
		maxResolution = 2;							//Just trying our luck with 2 milliseconds
	else
		maxResolution = timeCaps.wPeriodMin;

	if (maxResolution <= 2)
		requiredResolution = 2;
	else
		requiredResolution = maxResolution;


	timeBeginPeriod(requiredResolution);
	previousTime = timeGetTime();
}

FrameTimer::~FrameTimer() { timeEndPeriod(requiredResolution); }


bool FrameTimer::delayFrame() {
	currentTime = timeGetTime();
	if (currentTime - previousTime >= 16 || currentTime < previousTime) {
		previousTime = currentTime;
		return false;
	}
	return true;
}